home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- import pkg_resources
- from peak.util.decorators import struct
- from peak import context
- __all__ = [
- 'Hook',
- 'Extensible',
- 'PluginManager']
-
- def additional_tests():
- import doctest
- return doctest.DocFileSuite('README.txt', package = '__main__', optionflags = doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE)
-
-
- class Extensible(object):
- __slots__ = ()
- extend_with = ()
-
- def load_extensions(self):
- for ext in _flatten_callables(self.extend_with):
- ext(self)
-
-
-
-
- def _flatten_callables(ob):
- if callable(ob):
- yield ob
- else:
- for sub_ob in ob:
- for ob in _flatten_callables(sub_ob):
- yield ob
-
-
-
-
- class Hook(object):
- __slots__ = ()
-
- def __iter__(self):
- return PluginManager.iterHooks(self.group, self.impl)
-
-
- def register(self, ob, impl = None):
- if impl and self.impl and impl != self.impl:
- raise ValueError('Can only register ' + self.impl + ' implementations')
-
- if not impl:
- pass
- return PluginManager.addEntryPoint(self.group, self.impl, ob)
-
-
- def notify(self, *args, **kw):
- for hook in self.query(*args, **kw):
- pass
-
-
-
- def query(self, *args, **kw):
- if kw:
- for hook in self:
- yield hook(*args, **kw)
-
- elif args:
- for hook in self:
- yield hook(*args)
-
- else:
- for hook in self:
- yield hook()
-
-
-
- struct(Hook)
-
- def Hook(group, impl = None):
- return (group, impl)
-
- _implementations = { }
-
- class PluginManager(context.Service):
-
- def addEntryPoint(self, group, impl, ob):
- _implementations.setdefault(group, []).append((impl, ob))
-
-
- def iterHooks(self, group, impl = None, project = None):
- if project:
- project = project.lower()
-
- for name, ob in _implementations.get(group, ()):
- if impl and name != impl:
- continue
-
- yield ob
-
- for ep in pkg_resources.iter_entry_points(group, impl):
- yield ep.load()
-
-
-
-